<?php

namespace Xrfg\Api\Trading\Spot\Listeners;

use Xrfg\Api\Trading\Spot\Events\KlineSavedEvent;

class SaveKline
{
    protected $redis;

    public function __construct()
    {
        $this->redis = app('redis.connection');
    }

    public function handle(KlineSavedEvent $event)
    {
        $kline = $event->kline;
        $data = [
            'id' => $kline->time_open,
            'open' => $kline->price_open,
            'high' => $kline->price_high,
            'low' => $kline->price_low,
            'close' => $kline->price_close,
            'number' => $kline->trades_number,
            'total' => $kline->trades_total,
            'count' => $kline->trades_count,
        ];
        $key = "etf:kline:{$event->kline->symbol}:{$event->kline->type}";
        $this->redis->set($key, json_encode($data));
    }
}
